//
//  RDHttpSession.h
//  RDelivery
//
//  Created by selmerzhang on 2021/8/25.
//  Copyright © 2021 Tencent. All rights reserved.
//

#ifndef RDHttpSession_h
#define RDHttpSession_h

#import "RDConfigServerJsonModel.h"
#import "RDeliverySDKSettings.h"
#import "RDHttpRequest.h"
#import "RDeliveryPullConfigTask.h"
#import "RDConfigEventListener.h"
#import "RDeliverySystemBizDataDelegate.h"


NS_ASSUME_NONNULL_BEGIN

/// 日志tag
FOUNDATION_EXPORT NSString * const RDHttpSessionLogTag;

//Http状态码
FOUNDATION_EXPORT NSInteger const RDHttpStatusCodeSuccess;

@protocol RDHttpSessionDelegate <NSObject>

/// 计算子任务ID之和
- (NSString *)fetchSubTaskIdSum;

@end

@interface RDHttpSession : NSObject

/// SDK设置
@property(nonatomic, strong, readonly) RDeliverySDKSettings *settings;

/// 首次初始化时自己生成UUID并持久化，当guid为空时后台将取UUID作为灰度、抽样等逻辑的标识
@property(nonatomic, copy, readonly) NSString *uuid;

/// 是否完成读取context，如果没有，则hold住队列暂时不发包
@property(atomic, assign) BOOL hasLoadedContext;

/// 初始化
/// @param settings 设置信息
- (instancetype)initWithSettings:(RDeliverySDKSettings *)settings delegate:(_Nullable id<RDHttpSessionDelegate>)delegate;

/// 完成从磁盘中读取context
- (void)finishLoadingContext:(NSString * _Nullable)context;

/// 发起拉取配置请求
/// @param pullType 拉取类型
/// @param batchRequestStartTime 合并请求时间戳
/// @param keys 配置的key
/// @param sceneIds 场景id列表
/// @param source 请求来源
/// @param getConfigHandler 拉到配置后的回调
- (void)requestWithPullType:(RDConfigServerPullType)pullType
      batchRequestStartTime:(NSTimeInterval)batchRequestStartTime
                       keys:(NSArray<NSString *> * _Nullable)keys
                   sceneIds:(NSArray<NSNumber *> * _Nullable)sceneIds
              requestSource:(RDGetConfigRequestSource)source
            completeHandler:(RDPullConfigCompletedBlock)getConfigHandler;

/// 根据taskIds直接读取后台配置，配置不更新到本地
/// @param taskIds 配置任务Id
/// @param handler 数据回调
- (void)sendGetConfigRequestWithTaskIds:(NSArray<NSNumber *> *)taskIds
                        completeHandler:(void(^ _Nullable)(NSArray<RDConfigInfo *> *configInfoArray, NSError * _Nullable error))handler;

/// 切换用户id或环境时，重置参数
/// @param guid 用户id
/// @param envId 环境ID
/// @param context 请求版本信息
- (void)resetGuid:(NSString *)guid
              env:(NSString *)envId
          context:(NSString *)context;

/// 取消当前请求队列中所有请求
- (void)cancelAllRequest;

/// 为配置事件添加一个监听者
/// @param listener 监听者
- (void)addConfigEventListener:(id<RDConfigEventListener>)listener;

/// 移除配置事件监听者
/// @param listener 监听者
- (void)removeConfigEventListener:(id<RDConfigEventListener>)listener;

/// 发送请求
/// @param request 请求 Model
- (void)sendRequest:(RDHttpRequest *)request;

/// 获取当前平台
- (RDConfigServerPlatform)getPlatform;

/// 加密请求包
- (NSData *)encryptRequestData:(NSData *)data 
                 aesEncryptKey:(NSString *)aesEncryptKey;
/// 解密回包
- (NSData *)decryptResponseData:(NSData *)responseData
                  aesEncryptKey:(NSString *)aesEncryptKey
                failureCallback:(void(^ _Nullable)(NSError * _Nullable error))failureCallback;

@end

NS_ASSUME_NONNULL_END

#endif /* RDHttpSession_h */
